"""import Config
import MessageHandler
import MessageSender
import Logger
import Pinger
"""

import socket, os, random, time
import string 
import Server
from MessageSender import *
#from MessageHandler import MessageHandler
from Pinger import *
from Config import *
from Logger import *
#import MessageHandler


def server_tag(logger):
# TODO: aanmaken van serverTag 

    logger.add_queue("Generating unique tag", "main")
    pid    = os.getpid()
    length = 28 - len(str(pid))
    temp   = ''
    random.seed(time.time() * pid)
    for i in range(length):
        temp += random.choice(string.letters + string.digits)
    tag = str(pid) + temp
    logger.add_queue("Generated tag: " + tag , "main")
    return tag

if __name__ == "__main__":
    # make the socket
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        #sock.bind((str(CONTROL_SERVER_HOST), str(CONTROL_SERVER_PORT)))

        log = Logger()
        # Make the unique server tag
        tag = server_tag(log)
        
        server = Server.Server()
        server.tag = tag
        
        # Make threads
        logger = Logger()
        sender = MessageSender.MessageSender(logger, sock , server)
        handler = MessageHandler(logger, sender, server)
        pinger = Pinger(logger, handler, server)
        listener = Listener(handler, logger, sock)
             
        # start threads
        logger.start()
        sender.start()
        handler.start()
        listener.start()
        pinger.start()
        sender.add_queue(MessageHandler.create_message(601, 0, tag))
